﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

namespace DuLieu
{
    public class KetNoi_DL
    {
          SqlConnection con = new SqlConnection();

          string clsKetNoi = @"Data Source=THUY;Initial Catalog=QUANLYSINHVIEN;Integrated Security=False;User ID=sa;Password=123456;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False";

        public KetNoi_DL()
        {
            con.ConnectionString = clsKetNoi;
            if (con.State == System.Data.ConnectionState.Closed)
                con.Open();
        }
        public static SqlConnection ketnoi()
        {
            SqlConnection cnn = new SqlConnection( @"Data Source=THUY;Initial Catalog=QUANLYSINHVIEN;Integrated Security=False;User ID=sa;Password=123456;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False");
            return cnn;
        }
        public static DataSet getdata(string sql, string tenbang)
        {
            SqlConnection cnn = KetNoi_DL.ketnoi();
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(sql, cnn);
            da.Fill(ds, tenbang);
            return ds;
        }
        
        public DataTable LayDuLieu(string sql)
        {
            SqlCommand command = new SqlCommand(sql, con);
            command.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            return dt;
        }

        public SqlDataReader LayAnh(string sql, string[] name, object[] value, int Nparameter)
        {
            SqlCommand command = new SqlCommand(sql, con);
            command.CommandType = CommandType.StoredProcedure;
            for (int i = 0; i < Nparameter; i++)
            {
                command.Parameters.AddWithValue(name[i], value[i]);
            }
            
            SqlDataReader reader = command.ExecuteReader();
            return reader;
        }

        public DataTable TimKiem(string sql, string[] name, object[] value, int Nparameter)
        {
            SqlCommand command = new SqlCommand(sql, con);
            command.CommandType = CommandType.StoredProcedure;
            for (int i = 0; i < Nparameter; i++)
			{
                command.Parameters.AddWithValue(name[i], value[i]);
			}
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            return dt;
        }

        public int CapNhat(string sql)
        {
            SqlCommand command = new SqlCommand(sql, con);
            command.CommandType = CommandType.StoredProcedure;
            return command.ExecuteNonQuery();
        }

        public int CapNhat(string sql, string[] name, object[] value, int Nparameter)
        {
            SqlCommand command = new SqlCommand(sql, con);
            command.CommandType = CommandType.StoredProcedure;
            for (int i = 0; i < Nparameter; i++)
            {
                command.Parameters.AddWithValue(name[i], value[i]);
            }
            return command.ExecuteNonQuery();
        }
    }
    public class data : KetNoi_DL
    {
        public DataSet loadcombobox(string tenbang)
        {
            string sql = @"select * from " + tenbang;
            return getdata(sql, tenbang);
        }
    }
}
